<?php

class DbUtil {

    /**
     *
     * @param string $type One of the types of the is_* functions (http://de3.php.net/manual/de/ref.var.php)
     * @param array  $a    Array to filter
     * @return array       Filtered array 
     */
    static function filterArrayByType ($type, $a) {
        $filtered = array();
        $function = 'is_'.$type;
        foreach ($a as $n => $v) {
            if ($function($v)) {
                $filtered[$n] = $v;
            }
        }
        return $filtered;
    }

    /**
     * Regex
     * @param string $pattern 
     * @param array  $a       Array to filter
     * @return array          Filtered array 
     */
    static function filterArrayByRegex ($pattern, $a) {
        $filtered = array();
        foreach ($a as $n => $v) {
            if (preg_match($pattern, $v)) {
                $filtered[$n] = $v;
            }
        }
        return $filtered;
    }

    
    
/**
 * flattens a result array of a data base query like:
 *
 * array(
 * 	0 => array(
 *          "name" => "a name"
 *       ),
 * 	1 => array(
 *          "name" => "a second name"
 *       ),
 * 	2 => array(
 *          "name" => "a third name"
 *       ),
 * 	3 => array(
 *          "name" => "a fourth name"
 *       )
 * )
 *
 * to
 *
 * array("a name", "a second name", "a third name", "a fourth name")
 *
 */
    static function flattenArray ($a) {
        $flattened = array();
        foreach ($a as $n => $v) {
            if (is_array($v)) {
                $flattened = array_merge($flattened, DbUtil::flattenArray($v));
            } else {
                $flattened[] = $v;
            }
        }
        return $flattened;
    }

    static function comboBoxArray ($a) {
        $flattened = array();
        foreach ($a as $n => $v) {
            if (is_array($v)) {
                $flattened = array_merge($flattened, DbUtil::comboBoxArray($v));
            } else {
                $flattened[] = array($v, $v);
            }
        }
        return $flattened;
    }

    /**
     * Create and return a clean array with only fields of the specified form of the specified psp object.
     *
     * If $data includes a field named after a file field like <filefield>_delete, then the file will be deleted.
     *
     * @param array $data data array
     * @param phpinchains_psp_PSPPage $psp psp object
     * @param string $form form name
     * @param boolean $autohandlefile If true, fields '<filefield>_type', '<filefield>_size', and '<filefield>_filename' are automatically filled. Used for input fields of type 'file'.
     */
    static function cleanDataArray ($data, phpinchains_psp_PSPPage $psp, $form, $autohandlefile = false) {
        $d = array();
        $formarray = $psp->getFormFields($form);
        if (is_array($formarray))
            foreach ($formarray as $f) {
                $d[$f] = $data[$f];
            }
        if ($autohandlefile) {
            $files = $psp->getFileFormFields($form);
            if (is_array($files) && count($files)) {
                foreach ($files as $file) {
                // delete checkbox is checked
                    if ($data[$file.'_delete']) {
                        $d[$file]['type']     = "";
                        $d[$file]['name']     = "";
                        $d[$file]['tmp_name'] = "";
                        $d[$file]['size']     = "";
                        $d[$file.'_type']     = "";
                        $d[$file.'_filename'] = "";
                        $d[$file.'_size']     = "";
                    } else {
                        $farray = $data[$file];
                        if (empty($farray['tmp_name'])) {
                            unset($d[$file]);
                        } else {
                            $d[$file.'_type']     = $farray['type'];
                            $d[$file.'_filename'] = $farray['name'];
                            $d[$file.'_size']     = $farray['size'];
                        }
                    }
                }
            }
        }
        return $d;
    }

}

?>